package com.app.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.app.entity.*;
import com.app.service.*;

@Controller
@RequestMapping("/tests")
public class TestsController {
    @Resource
    TestsService testsService;


    @Resource
    CatesService catesService;

    @RequestMapping("/report")
    public ModelAndView report(Model model, HttpSession sess, String userid) {

        ModelAndView mav = new ModelAndView("admin/report");
        List<Tests> list2 = testsService.getTests(userid);
        Double avg =0D;
        Double total=0D;
        for (Tests t : list2
        ) {
            List<Cates> ls = catesService.getRange(Integer.parseInt(t.getTotal()));
            if (ls.size() > 0) {
                t.setC(ls.get(0));
            }
            total+=Integer.parseInt(t.getTotal());
        }
        if(list2.size()>0)
        avg=total/list2.size();

        Cates md = null;
        int ma = avg.intValue();
        List<Cates> ls = catesService.getRange(ma);
        if (ls.size() > 0) {
            md = ls.get(0);
        }

        System.out.println("Avg"+avg);
        model.addAttribute("list2", list2);
        model.addAttribute("md", md);
        return mav;
    }

    @RequestMapping("/tests/add")
    public String testsAdd() {
        return "admin/tests/add";
    }

    @RequestMapping(value = "/tests/save", method = RequestMethod.POST)
    public String testsSave(String token,
                            String uid,
                            String qid,
                            String score,
                            String total,
                            String created,
                            String rest,
                            String describ,
                            Model model) {
        Tests modelX = new Tests();
        modelX.setToken(token);
        modelX.setUid(uid);
        modelX.setQid(qid);
        modelX.setScore(score);
        modelX.setTotal(total);
        modelX.setCreated(created);
        modelX.setRest(rest);
        modelX.setDescrib(describ);

        testsService.insert(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "Success!");
        return "admin/tests/add";
    }

    @RequestMapping("/tests/delete")
    public String testsDelete(int id) {
        testsService.delete(id);
        return "redirect:list";
    }

    @RequestMapping("/tests/list")
    public ModelAndView testsList() {
        List<Tests> testsList = testsService.getAllTests();
        System.out.println(testsList.size());
        ModelAndView mav = new ModelAndView("admin/tests/list");
        mav.addObject("list", testsList);
        return mav;
    }

    @RequestMapping("/tests/edit")
    public String testsEdit(String id, Model model) {
        Tests tests = testsService.getTestsById(Integer.parseInt(id));
        model.addAttribute("model", tests);
        return "admin/tests/edit";
    }

    @RequestMapping(value = "/tests/update", method = RequestMethod.POST)
    public String testsUpdate(String token,
                              String uid,
                              String qid,
                              String score,
                              String total,
                              String created,
                              String rest,
                              String describ,
                              int id,
                              Model model) {

        Tests modelX = testsService.getTestsById(id);
        modelX.setToken(token);
        modelX.setUid(uid);
        modelX.setQid(qid);
        modelX.setScore(score);
        modelX.setTotal(total);
        modelX.setCreated(created);
        modelX.setRest(rest);
        modelX.setDescrib(describ);

        testsService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("model", modelX);
        model.addAttribute("message", "Success");
        return "admin/tests/edit";
    }
}
